/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q2540.java
 * @Author : WangRuoyu
 * @Date 2023/1/29 10:48
 */

package leetcode;

public class Q2540 {
    public int getCommon(int[] nums1, int[] nums2) {
        int res = -1;

        int index1 = 0;
        int index2 = 0;

        while ((index2 < nums2.length) && (index1 < nums1.length)) {
            if (nums1[index1] > nums2[index2]) {
                index2++;
            } else if (nums1[index1] < nums2[index2]) {
                index1++;
            } else {
                return nums1[index1];
            }
        }

        return res;
    }

    public static void main(String[] args) {
        Q2540 sl = new Q2540();
        int[] nums1 = {1, 2, 3};
        int[] nums2 = {2, 4};
        System.out.println(sl.getCommon(nums1, nums2));
    }
}